package cn.xz.zysjz.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.LinearLayoutCompat;

import cn.xz.zysjz.R;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 弹窗*/
public class ShowDialog {

    public static void show(String s, AppCompatActivity appCompatActivity,OnShowClick onShowClick){
        // 1. 布局文件转换为View对象
        LayoutInflater inflater = LayoutInflater.from(appCompatActivity);
        View layout = inflater.inflate(R.layout.item_show_dialog, null);

        // 2. 新建对话框对象
        final Dialog dialog = new AlertDialog.Builder(appCompatActivity).create();
        dialog.setCancelable(true);
        dialog.show();
        dialog.getWindow().setContentView(layout);

        TextView tvTxt = layout.findViewById(R.id.tv_txt);
        tvTxt.setText(s);

        TextView tvBtn = layout.findViewById(R.id.tv_btn);
        tvBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                onShowClick.click();
            }
        });

    }

    public static void show1(String s, Context appCompatActivity, OnShowClick onShowClick){
        // 1. 布局文件转换为View对象
        LayoutInflater inflater = LayoutInflater.from(appCompatActivity);
        View layout = inflater.inflate(R.layout.item_sure_dialog, null);

        // 2. 新建对话框对象
        final Dialog dialog = new AlertDialog.Builder(appCompatActivity).create();
        dialog.setCancelable(true);
        dialog.show();
        dialog.getWindow().setContentView(layout);

        TextView tvTxt = layout.findViewById(R.id.tv_txt);
        tvTxt.setText(s);

        //取消
        TextView tvBtn0 = layout.findViewById(R.id.tv_btn0);
        tvBtn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        //确认
        TextView tvBtn1 = layout.findViewById(R.id.tv_btn1);
        tvBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                onShowClick.click();
            }
        });

    }

    public static void show2(String s, Context appCompatActivity, OnShowClickStr onShowClick){
        // 1. 布局文件转换为View对象
        LayoutInflater inflater = LayoutInflater.from(appCompatActivity);
        View layout = inflater.inflate(R.layout.item_sure_dialog1, null);

        // 2. 新建对话框对象
        AlertDialog dialog = new AlertDialog.Builder(appCompatActivity).create();
        dialog.setCancelable(false);
        dialog.setView(new EditText(appCompatActivity));
        dialog.show();
        dialog.getWindow().setContentView(layout);
        EditText tvTxt = layout.findViewById(R.id.tv_txt);
        tvTxt.setHint(s);

        //取消
        TextView tvBtn0 = layout.findViewById(R.id.tv_btn0);
        tvBtn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        //确认
        TextView tvBtn1 = layout.findViewById(R.id.tv_btn1);
        tvBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tvTxt.getText().toString().equals("")){
                    ToastUtils.show("昵称不能为空");
                    return;
                }
                dialog.dismiss();
                onShowClick.click(tvTxt.getText().toString());
            }
        });

    }


    public static void show3(Context appCompatActivity, OnShowClickStr onShowClick){
        // 1. 布局文件转换为View对象
        LayoutInflater inflater = LayoutInflater.from(appCompatActivity);
        View layout = inflater.inflate(R.layout.item_sure_dialog2, null);

        // 2. 新建对话框对象
        AlertDialog dialog = new AlertDialog.Builder(appCompatActivity).create();
        dialog.setCancelable(false);
        dialog.setView(new EditText(appCompatActivity));
        dialog.show();
        dialog.getWindow().setContentView(layout);
        EditText tvTxt = layout.findViewById(R.id.tv_txt);

        //取消
        TextView tvBtn0 = layout.findViewById(R.id.tv_btn0);
        tvBtn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        //确认
        TextView tvBtn1 = layout.findViewById(R.id.tv_btn1);
        tvBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tvTxt.getText().toString().equals("")){
                    ToastUtils.show("内容不能为空");
                    return;
                }
                dialog.dismiss();
                onShowClick.click(tvTxt.getText().toString());
            }
        });

    }

    public static void show4(Context appCompatActivity, OnShowClickPf onShowClick){
        // 1. 布局文件转换为View对象
        LayoutInflater inflater = LayoutInflater.from(appCompatActivity);
        View layout = inflater.inflate(R.layout.item_sure_dialog4, null);

        // 2. 新建对话框对象
        AlertDialog dialog = new AlertDialog.Builder(appCompatActivity).create();
        dialog.setCancelable(true);
        dialog.setView(new EditText(appCompatActivity));
        dialog.show();
        dialog.getWindow().setContentView(layout);

        //取消
        TextView tvBtn0 = layout.findViewById(R.id.tv_btn0);
        tvBtn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        LinearLayoutCompat ll1 = layout.findViewById(R.id.llc_1);
        LinearLayoutCompat ll5 = layout.findViewById(R.id.llc_5);
        LinearLayoutCompat ll10 = layout.findViewById(R.id.llc_10);

        ll1.setOnClickListener(v->{
            dialog.dismiss();
            onShowClick.click(1);
        });
        ll5.setOnClickListener(v->{
            dialog.dismiss();
            onShowClick.click(5);
        });
        ll10.setOnClickListener(v->{
            dialog.dismiss();
            onShowClick.click(10);
        });
    }


    //回调
    public interface OnShowClick {
        void click();
    }

    //回调
    public interface OnShowClickStr {
        void click(String s);
    }

    public interface OnShowClickPf {
        void click(int i);
    }
}
